package cn.leonis.leetcode;

/**
 * @author Tobi
 * @time 2021-05-27 3:46 PM
 * 测试Java是按值传递还是按引用传递,结果显示：Java是按值调用
 */
public class Test01 {

    public static void main(String[] args) {
        // int i = 0;
        // test01(i);
        // System.out.println("i = " + i);
        Person p1 = new Person("zhangsan");
        Person p2 = new Person("lisi");
        System.out.println("p1 = " + p1);
        System.out.println("p2 = " + p2);

        System.out.println("----swap-----");
        // 在调用函数的时候，会创建p1和p2的副本，最终发生交换的副本，而原本的值并没有发生改变
        swap(p1, p2);
        System.out.println("p1 = " + p1);
        System.out.println("p2 = " + p2);
    }

    public static void test01(int i) {
        i++;
    }

    public static void swap(Person x, Person y) {
        Person temp = x;
        x = y;
        y = temp;
    }
}

class Person {
    public String name;
    public Person(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                '}';
    }
}
